<template>
  <div class="home">
    <el-button type="primary" @click="dialogVisible1 = true">+ 新增学生</el-button>
    <el-dialog
      title="新增学生"
      :visible.sync="dialogVisible1"
      width="30%"
      :before-close="handleClose">
      <label for="textInput">学生姓名：</label>
      <el-input
        type="text"
        placeholder="请输入学生姓名"
        v-model="textInput.name"
        maxlength="10"
        show-word-limit>
      </el-input>
      <label for="studentId">学生ID：</label>
      <el-input
        type="text"
        placeholder="请输入学生ID"
        v-model="textInput.studentId"
        maxlength="10"
        show-word-limit>
      </el-input>
      <label for="className">班级：</label>
      <el-input
        type="text"
        placeholder="请输入班级"
        v-model="textInput.className"
        maxlength="10"
        show-word-limit>
      </el-input>
      <label for="telephone">电话：</label>
      <el-input
        type="text"
        placeholder="请输入电话"
        v-model="textInput.telephone"
        maxlength="15"
        show-word-limit>
      </el-input>
      <label for="qq">QQ：</label>
      <el-input
        type="text"
        placeholder="请输入QQ"
        v-model="textInput.qq"
        maxlength="15"
        show-word-limit>
      </el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="handleAdd">确 定</el-button>
      </span>
    </el-dialog>
    <br /><br />
    <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%;" :stripe="true"
      :border="true" max-height="500" :header-cell-style="{'text-align':'center'}"
      :cell-style="{'text-align':'center'}">
      <el-table-column prop="id" label="序号" width="120">
        <template slot-scope="scope">{{ scope.$index + 1 }}</template>
      </el-table-column>
      <el-table-column prop="name" label="学生姓名" width="240">
      </el-table-column>
      <el-table-column prop="studentId" label="学生ID" width="240">
      </el-table-column>
      <el-table-column prop="className" label="班级" width="240">
      </el-table-column>
      <el-table-column prop="telephone" label="电话" width="240">
      </el-table-column>
      <el-table-column prop="qq" label="QQ" width="240">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" plain @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button size="mini" type="danger" plain @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="修改学生信息"
      :visible.sync="dialogVisible2"
      width="30%"
      :before-close="handleClose">
      <label for="textInput">学生姓名：</label>
      <el-input
        type="text"
        placeholder="请输入学生姓名"
        v-model="textInput.name"
        maxlength="10"
        show-word-limit>
      </el-input>
      <label for="studentId">学生ID：</label>
      <el-input
        type="text"
        placeholder="请输入学生ID"
        v-model="textInput.studentId"
        maxlength="10"
        show-word-limit>
      </el-input>
      <label for="className">班级：</label>
      <el-input
        type="text"
        placeholder="请输入班级"
        v-model="textInput.className"
        maxlength="10"
        show-word-limit>
      </el-input>
      <label for="telephone">电话：</label>
      <el-input
        type="text"
        placeholder="请输入电话"
        v-model="textInput.telephone"
        maxlength="11"
        show-word-limit>
      </el-input>
      <label for="qq">QQ：</label>
      <el-input
        type="text"
        placeholder="请输入QQ"
        v-model="textInput.qq"
        maxlength="10"
        show-word-limit>
      </el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="handleUpdate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getAllStudents, addStudent, deleteStudentById, getStudentById, updateStudent } from '@/api/student'

export default {
  name: 'StudentIndex',
  data () {
    return {
      tableData: [],
      dialogVisible1: false,
      dialogVisible2: false,
      textInput: {
        name: '',
        studentId: '',
        className: '',
        telephone: '',
        qq: ''
      },
      textId: ''
    }
  },
  methods: {
    async tableInit () {
      const res = await getAllStudents()
      this.tableData = res.data
    },
    async handleEdit (id) {
      const res = await getStudentById(id)
      this.textInput = res.data
      this.textId = id
      this.dialogVisible2 = true
    },
    handleDelete (id) {
      this.$confirm('此操作将永久删除该学生, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await deleteStudentById(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.tableInit()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleClose () {
      this.dialogVisible1 = false
      this.dialogVisible2 = false
      this.textInput = {
        name: '',
        studentId: '',
        className: '',
        telephone: '',
        qq: ''
      }
      this.textId = ''
    },
    async handleAdd () {
      await addStudent(this.textInput)
      this.$message.success('添加成功')
      this.handleClose()
      this.tableInit()
    },
    async handleUpdate () {
      await updateStudent(this.textId, this.textInput)
      this.$message.success('修改成功')
      this.handleClose()
      this.tableInit()
    }
  },
  async created () {
    this.tableInit()
  }
}
</script>

<style scoped lang="less">
</style>
